package edu.wata.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import edu.wata.domain.Order;
import edu.wt.dbutil.DBConnection;

public class OrderDao {
	public List<Order> queryName(String userName) {
		String sql = "select * from orderform where name=?";
		Connection connection = DBConnection.getConnection();
		List<Order> list = new ArrayList<>();
		try {
			PreparedStatement psmt = connection.prepareStatement(sql);
			psmt.setString(1, userName);
			ResultSet rs = psmt.executeQuery();
			while (rs.next()) {
				Order order = new Order(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getDate(4).toLocalDate(),
						rs.getBigDecimal(5), rs.getBigDecimal(6));
				list.add(order);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	public int deleteTicket(String userName, String trianId) {
		Connection conn = null;
		PreparedStatement psmt = null;
		int rowNum = 0;
		String sql = "delete from tts_orderform where userName=? and trainid=?";
		conn = DBConnection.getConnection();
		try {
			psmt = conn.prepareStatement(sql);
			psmt.setString(1, userName);
			psmt.setString(2, trianId);
			rowNum = psmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rowNum;
	}

}
